<template>
	<view class="user" :class="isDark?'dark':''" :style="{'background-color':isDark?'#1c1c1c':'#f6f6f6','min-height':isDark?'100vh':'auto'}">
		<view class="header" :style="[{height:CustomBar + 'px'}]">
			<view class="cu-bar" :style="{'height': CustomBar + 'px','padding-top':StatusBar + 'px'}">
				<view class="action" @tap="back">
					<text class="cuIcon-back"></text>
				</view>
				<view class="content text-bold" :style="[{top:StatusBar + 'px'}]">
					{{$t('sygpt.home.ailargemodel')}}
				</view>
				<view class="action">
					<text class="text-blue" @tap="toLink('/pages/plugins/sy_gpt/all')">{{$t('sygpt.home.viewall')}}</text>
				</view>
			</view>
		</view>
		<view class="gpt-bg" v-if="!isDark">
			<image src="./style/gpt-bg.png"></image>
		</view>
		<view :style="[{padding:NavBar + 'px 10px 0px 10px'}]"></view>
		
		<view class="cu-list menu-avatar gptList" style="margin-top: 20upx;">
			<view class="search-type gpt-search-type">
				<view class="search-type-box" @tap="setType(0)" :class="type==0?'gpt-act':''">
					<text>{{$t('sygpt.home.chatgpt')}}</text>
				</view>
				<view class="search-type-box" @tap="setType(1)" :class="type==1?'gpt-act':''">
					<text>{{$t('sygpt.home.aiapp')}}</text>
				</view>
			</view>
			<view class="no-data" v-if="gptList.length==0&&dataLoad">
				<text class="cuIcon-text"></text>{{$t('common.nodata')}}
			</view>
			<view class="dataLoad" v-if="!dataLoad">
				<image src="@/static/loading.gif"></image>
			</view>
			<view class="cu-item gpt-item" v-for="(item,index) in gptList" :key="index">
				<view class="cu-avatar round" :style="item.style"></view>
				<view class="content">
					<view class="text-black">
						<text class="text-pink margin-right-xs" v-if="item.isVip==1">{{$t('global.vip')}}</text>{{item.name}}
					</view>
					<view class="text-gray text-sm flex margin-top-xs">
						{{$t('sygpt.home.singlereq') + ':'}}<text class="text-orange">{{item.price}}</text>{{currencyName}}
					</view>
				</view>
				<view class="action goUserIndex">
					<view class="cu-btn bg-blue light" @tap="goChat(item)">
						<text class="cuIcon-messagefill margin-right-xs"></text>{{$t('sygpt.home.interact')}}
					</view>
				</view>
			</view>
			<!-- <view class="load-more" @tap="loadMore" v-if="gptList.length>=limit">
				<text>{{moreText}}</text>
			</view> -->
		</view>
		<!--加载遮罩-->
		<view class="loading" v-if="isLoading==0">
			<view class="loading-main">
				<image src="@/static/loading.gif"></image>
			</view>
		</view>
		<!--加载遮罩结束-->
	</view>
</template>

<script>
	import { localStorage } from '@/js_sdk/mp-storage/mp-storage/index.js'
	import darkModeMixin from '@/utils/darkModeMixin.js'
	export default {
		mixins: [darkModeMixin],
		data() {
			return {
				StatusBar: this.StatusBar,
				CustomBar: this.CustomBar,
				NavBar:this.StatusBar +  this.CustomBar,
				AppStyle:this.$store.state.AppStyle,
				
				gptList:[],
				searchText:"",
				type:0,
				
				page:1,
				limit:10,
				moreText:$t('common.loadmoretxt'),
				isLoad:0,
				isLoading:0,
				currencyName:"",
				dataLoad:false,
			}
		},
		onPullDownRefresh(){
			var that = this;
			that.page=1;
			that.getGptList(false);
			setTimeout(function () {
				uni.stopPullDownRefresh();
			}, 1000);
		},
		onReachBottom() {
		    //触底后执行的方法，比如无限加载之类的
			var that = this;
			if(that.isLoad==0){
				that.loadMore();
			}
		},
		onShow(){
			var that = this;
			that.page=1;
			// #ifdef APP-PLUS
			
			//plus.navigator.setStatusBarStyle("dark")
			// #endif
			
		},
		onLoad() {
			var that = this;
			// #ifdef APP-PLUS || MP
			that.NavBar = that.CustomBar;
			// #endif
			that.getGptList(false);
			uni.request({
				url:that.$API.SPset(),
				method:'GET',
				dataType:"json",
				success(res) {
					that.currencyName = res.data.assetsname;
				},
				fail(error) {
					
				}
				
			})
		},
		methods:{
			allCache(){
				var that = this;
			},
			back(){
				uni.navigateBack({
					delta: 1
				});
			},
			searchTag(){
				var that = this;
				var searchText = that.searchText;
				that.page=1;
				that.getGptList();
			
			},
			setType(type){
				var that = this;
				that.gptList = [];
				that.type = type;
				that.page=1;
				that.getGptList();
			},
			searchClose(){
				var that = this;
				that.searchText = "";
				that.page=1;
				that.getGptList();
			
			},
			formatDate(datetime) {
				var datetime = new Date(parseInt(datetime * 1000));
				// 获取年月日时分秒值  slice(-2)过滤掉大于10日期前面的0
				var year = datetime.getFullYear(),
					month = ("0" + (datetime.getMonth() + 1)).slice(-2),
					date = ("0" + datetime.getDate()).slice(-2),
					hour = ("0" + datetime.getHours()).slice(-2),
					minute = ("0" + datetime.getMinutes()).slice(-2);
				//second = ("0" + date.getSeconds()).slice(-2);
				// 拼接
				var result = year + "-" + month + "-" + date + " " + hour + ":" + minute;
				// 返回
				return result;
			},
			loadMore(){
				var that = this;
				that.moreText = that.$t('global.loading');
				that.isLoad=1;
				that.getGptList(true);
				
			},
			getGptList(isPage){
				var that = this;
				that.dataLoad = false;
				var page = that.page;
				if(isPage){
					page++;
				}
				var token = ""
				if(localStorage.getItem('userinfo')){
					var userInfo = JSON.parse(localStorage.getItem('userinfo'));
					token=userInfo.token;
				
				}
				var data = {
					"type":that.type
				}
				that.$Net.request({
					url: that.$API.gptList(),
					data:{
						"searchParams":JSON.stringify(that.$API.removeObjectEmptyKey(data)),
						"limit":that.limit,
						"searchKey":that.searchText,
						"page":page,
						"order":"created",
						"token":token
					},
					header:{
						'Content-Type':'application/x-www-form-urlencoded'
					},
					method: "get",
					dataType: 'json',
					success: function(res) {
						that.isLoad=0;
						that.dataLoad = true;
						if(res.data.code==1){
							var list = res.data.data;
							if(list.length>0){
								
								var gptList = [];
								for(var i in list){
									var arr = list[i];
									arr.style = "background-image:url("+list[i].avatar+");"
									gptList.push(arr);
								}
								if(isPage){
									that.page++;
									that.gptList = that.gptList.concat(gptList);
								}else{
									that.gptList = gptList;
								}
							}else{
								if(isPage){
									that.moreText = that.$t('common.nomoredata');
								}else{
									that.gptList = [];
								}
								
							}
						}
						var timer = setTimeout(function() {
							that.isLoading=1;
							clearTimeout('timer')
						}, 300)
					},
					fail: function(res) {
						that.dataLoad = true;
						that.isLoad=0;
						that.moreText = that.$t('common.loadmoretxt');
						var timer = setTimeout(function() {
							that.isLoading=1;
							clearTimeout('timer')
						}, 300)
					}
				})
			},
			toLink(text){
				var that = this;
				if(!localStorage.getItem('token')||localStorage.getItem('token')==""){
					uni.showToast({
						title: that.$t('common.plslogin'),
						icon: 'none'
					})
					return false;
				}
				uni.navigateTo({
					url: text
				});
			},
			goChat(data){
				var that = this;
				if(!localStorage.getItem('token')||localStorage.getItem('token')==""){
					uni.showToast({
						title: that.$t('common.plslogin'),
						icon: 'none'
					})
					return false;
				}
				if(data.type==0){
					uni.navigateTo({
					    url: '/pages/plugins/sy_gpt/chat?id='+data.id
					});
				}else{
					uni.navigateTo({
					    url: '/pages/plugins/sy_gpt/app?id='+data.id
					});
				}
				
			},
			subText(text,num){
				if(text){
					if(text.length>num){
						text = text.substring(0,num);
						return text+"……";
					}else{
						return text;
					}
				}else{
					return this.$t('sygpt.home.mgrnosetintro')
				}
			},
			
			
		}
	}
	
</script>

<style>
</style>
